package com.shop.manager.controller;

//import com.shop.manager.entity.Brand;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.Brand;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.BrandService;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description 品牌信息增删改查Controller层
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    /**
     * @Description 注入service对象
     */
    @Resource(type = BrandService.class)
    private BrandService brandService;

    /**
     * @Description 添加品牌信息
     * @param brand 品牌对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HttpResult insertAttr(@RequestBody @ApiParam(value = "TbAttribute 对象") Brand brand) {
        try {
            brandService.add(brand);
            //添加修改成功
            return HttpResult.ok("添加品牌成功!");
        } catch (Exception e) {
            //添加异常
            return HttpResult.error("添加品牌失败!");
        }
    }

    /**
     * @Description 批量删除品牌信息
     * @param ids 品牌id的数组
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public HttpResult delete(@RequestBody(required = false) Integer[] ids) {
        try {
            brandService.deleteByIds(ids);
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改品牌信息
     * @param brand 修改的品牌对象
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public HttpResult update(@RequestBody(required = false) @ApiParam(value = "Brand 对象") Brand brand) {
        try {
            brandService.update(brand);
            return HttpResult.ok("修改品牌成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改品牌失败!");
    }

    /**
     * @Description 根据id查询品牌信息(修改回显)
     * @param brandId 品牌id
     * @return
     */
    @RequestMapping(value = "/edit/{brandId}", method = RequestMethod.GET)
    public HttpResult updateEdit(@PathVariable Integer brandId) {
        return HttpResult.ok(brandService.findOne(brandId));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param brand 查询条件对象
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    public HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) @ApiParam(value = "分页条件Brand对象") Brand brand) {
        try {
            //分页查询
            PageResult result = brandService.findPage(pageNum, size, brand);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询品牌异常");
        }
    }

    /**
     * @Description 分页查询
     * @param page 当前页码
     * @param rows 当前页条数
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public PageResult findAllAttr(@RequestParam("page") Integer page, @RequestParam("rows") Integer rows) {
        PageResult pageResult = brandService.findPage(page, rows);
        return pageResult;
    }

    /**
     * @Description 模糊查询品牌信息
     * @param brand 品牌对象
     * @return
     */
    @RequestMapping(value = "/findByWhere", method = RequestMethod.POST)
    public HttpResult findByWhere(@RequestBody(required = false) @ApiParam(value = "Brand 对象") Brand brand) {
        return HttpResult.ok(brandService.findByWhere(brand));
    }




}
